﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace OneSchema
{
    public class FD
    {
        private AttributeSet m_LHS, m_RHS;

        public FD(IEnumerable<string> LHS, IEnumerable<string> RHS)
            : this(new AttributeSet(LHS), new AttributeSet(RHS))
        { }

        public FD(string commaDelimitedLHS, string commaDelimitedRHS)
            : this(new AttributeSet(commaDelimitedLHS), new AttributeSet(commaDelimitedRHS))
        { }

        public FD(AttributeSet LHS, AttributeSet RHS)
        {
            m_LHS = LHS;
            m_RHS = RHS;

            // Normalize FDs such that RHS = RHS U LHS
            m_RHS.UnionWith(m_LHS);
        }

        public bool IsTrivial()
        {
            return m_RHS.IsSubsetOf(m_LHS);
        }

        public AttributeSet GetUnusedAttributes()
        {
            AttributeSet retSet = new AttributeSet(m_RHS);
            retSet.Complement();
            return retSet;
        }

        public AttributeSet GetUsedAttributes()
        {
            return new AttributeSet(m_RHS);
        }

        public AttributeSet LHS { get { return m_LHS; } }
        public AttributeSet RHS { get { return m_RHS; } }

        public override string ToString()
        {
            return string.Format("{0}->{1}", m_LHS.ToString(), m_RHS.ToString());
        }

        public override bool Equals(object obj)
        {
            // Check for null values and compare run-time types.
            if (obj == null || GetType() != obj.GetType())
                return false;

            FD fd = (FD)obj;
            return ToString() == fd.ToString();
        }
    }
}
